var mysql = require('mysql');
var fs = require('fs');

var dbUtil = function () {
    this.init = function (params) {
        console.info(params);
        console.info(Object.keys(params).length);
        if(Object.keys(params).length===0){
            console.error('param error.');
            return null;
        }
        var host = params.host;
        var user = params.user;
        var password = params.password;
        var db = params.db;
        var port = params.port;

        if(host==null || host==''){
            console.error('host error.');
            return null;
        }
        if(user==null || user==''){
            console.error('user error.');
            return null;
        }
        if(password==null || password==''){
            console.error('password error.');
            return null;
        }
        if(db==null || db==''){
            console.error('db name error.');
            return null;
        }
        if(port==null || port==''){
            port = '3306';
        }

        return mysql.createConnection({
            host: host,
            user: user,
            password: password,
            // database: db,
            port: port
        });

    };
    this.connect = function(connection, cb){
        var result = 0;
        connection.connect(function (err) {
            if(err){
                console.error('connect fail:' + err);
                result = -1;
            }else{
                console.info('connect success.');
                result = 1;
            }
            cb(result);
        });
    };
    this.initDB = function (connection, dbname, cb) {
        var result = 0;
        connection.query('create database ' + dbname, function (err) {
            if(err){
                console.error('create database fail:' + err);
                result = -1;
                cb(result);
            }else{
                connection.query('use ' + dbname, function (err) {
                    if(err) {
                        console.error('use database fail:' + err);
                        result = -2;
                        cb(result);
                    }else{
                        connection.query('CREATE TABLE s_user (id INT (11) NOT NULL auto_increment,' +
                            ' NAME VARCHAR (100) NOT NULL, ' +
                            ' PASSWORD VARCHAR (100) NOT NULL, ' +
                            ' nickname VARCHAR (200) NOT NULL, ' +
                            ' gender INT (2), ' +
                            ' STATUS INT (2), ' +
                            ' logintime date, ' +
                            ' createby VARCHAR (100), ' +
                            ' createtime date, ' +
                            ' modifyby VARCHAR (100), ' +
                            ' modifytime date, ' +
                            ' PRIMARY KEY (id) )'
                            , function (err) {
                                if(err){
                                    console.error('create table fail:' + err);
                                    result = -2;
                                }else{
                                    result = 1;
                                }
                                cb(result);
                            });
                    }
                });
            }
        });
    }
};

module.exports=dbUtil;